/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: userOperate.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月22日 星期一 03时08分37秒
***************************************************/

#include "userOperate.h"

//会员操作系统函数
void userOperateSystem(int n){
	int model = 0;
	
	logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model info]:%s",\
			__FILE__,__FUNCTION__,__LINE__,strerror(errno),"会员操作系统");
	system("clear");
	while(1){
		printf("---会员操作系统---\n");
		printf("1--输出信息\n");
		printf("2--充值\n");
		printf("0--退出\n");

		printf("model=");
		scanf("%d",&model);

		if(model == 0){
			logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
					__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"0--退出");
			system("clear");
			break;
		}
		switch(model){
			case 1:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"1--输出信息");
				outputUserToVipInfo(n);
				break;
			case 2:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"2--充值");
				rechargeUser(n);
				break;
			default:
				logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s [model]:%d [model info]:%s",\
						__FILE__,__FUNCTION__,__LINE__,strerror(errno),model,"输入错误！");
				printf("输入错误！\n");
				break;
		}
	}
}
//输出会员用户信息函数  输入：用户id
void outputUserToVipInfo(int n){
	FILE *fp = NULL;
	VIP_M *head = NULL;
	queryIdToVipInfo(fp,head,n);
}
//会员充值函数
void rechargeUser(int n){
	FILE *fp = NULL;
	VIP_M *head = NULL;
	head = createManagerHead();
	fp = fopen("./vipInfo.txt","r");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	head = vipInfoReadFile(fp,head);
	fclose(fp);

	float num = 0;
	VIP_M *cur = NULL;
	cur = head->next;
	while(cur != NULL){
		if(cur->data.id == n){
			printf("请输入充值金额：");
			scanf("%f",&num);
			if(num > 0){
				cur->data.balance += num;
			}else{
				printf("充值金额输入错误！\n");
			}
			break;
		}
		cur = cur->next;
	}

	fp = fopen("./vipInfo.txt","w");
	if(fp == NULL){
		logMassage(LOG_INFO,"[file]:%s [function]:%s [line]:%d [err]:%s",\
				__FILE__,__FUNCTION__,__LINE__,strerror(errno));
		exit(-1);
	}

	fp = vipInfoWriteToFile(fp,head);

	freeManagerLink(head);
	fclose(fp);
}
